#include "PBStrDispose.h"
#include <string.h>

int PBudStr_read_socket(PBufStr *str, BSocketFile *file, char *endstr, unsigned int endstr_len) {
    unsigned char ch;
    int ret;
    while (1) {
        ret = BSocketFile_read(file, &ch);                                  //读取一个字符
        if (ret == BSOCKETFILE_RET_SUCCEED) {
            ret = PBufStr_char_add(str, (char) ch);                         //保存字符
            if (ret != PBUFSTR_RET_SUCCEED) {
                return ret;
            } else {
                ret = strcmp(str->buf + str->str_len - endstr_len, endstr);
                if (ret == 0)
                    return PBUFSTR_RET_SUCCEED;
            }
        } else {
            break;
        }
    }
    return ret;
}

int PBudStr_read_socket_char(PBufStr *str, BSocketFile *file, char endch) {
    int ret;
    unsigned char ch;
    ret = BSocketFile_read(file, &ch);
    if (ret == BSOCKETFILE_RET_SUCCEED) {
        ret = PBufStr_char_add(str, (char) ch);
        if (ret == PBUFSTR_RET_SUCCEED) {
            if (ch == endch) {
                return PBUFSTR_RET_SUCCEED;
            } else {
                return BSOCKETFILE_RET_WAIT;
            }
        }
    }
    return ret;
}